/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Text;

using Microsoft.VisualStudio.Shell.Interop;

namespace DslEditorPowerToy.Controls.Languages
{
    /// <summary>
    /// A class to provider ISelectionService limited functionality from ISelectionContainer
    /// </summary>
    public class SelectionContainerSelectionService : ISelectionService
    {
        private ISelectionContainer _selectionContainer = null;

        #region Constructors
        [CLSCompliant(false)]
        public SelectionContainerSelectionService(ISelectionContainer container)
        {
            this._selectionContainer = container;
        }
        #endregion

        #region ISelectionService Members

        public bool GetComponentSelected(object component)
        {
            ICollection selectedComponents = this.GetSelectedComponents();
            if (null == selectedComponents)
                return false;

            //Scan container for matching component
            ArrayList list = new ArrayList(selectedComponents);
            foreach (object selectedComponent in list)
            {
                if (component == selectedComponent)
                    return true;
            }

            return false;
        }

        public System.Collections.ICollection GetSelectedComponents()
        {
            //Return selected objects from container
            uint count = 0;
            if (0 == this._selectionContainer.CountObjects((uint)Constants.GETOBJS_SELECTED, out count))
            {
                if (count > 0)
                {
                    object[] objects = new object[count];
                    if (0 == this._selectionContainer.GetObjects((uint)Constants.GETOBJS_SELECTED, count, objects))
                    {
                        if (null != objects)
                            return new List<object>(objects);
                    }
                }
            }

            return null;
        }

        public object PrimarySelection
        {
            get
            {
                //Return first selected item (from container)
                ICollection selectedComponents = GetSelectedComponents();
                if (null != selectedComponents)
                {
                    if (selectedComponents.Count > 0)
                        return new ArrayList(selectedComponents)[0];
                }

                return null;
            }
        }

        public event EventHandler SelectionChanged;

        public event EventHandler SelectionChanging;

        public int SelectionCount
        {
            get
            {
                //Returns selected count from container
                uint count = 0;
                if (0 == this._selectionContainer.CountObjects((uint)Constants.GETOBJS_SELECTED, out count))
                    return (int)count;

                return 0;
            }
        }

        public void SetSelectedComponents(System.Collections.ICollection selectedComponents, SelectionTypes selectionType)
        {
            throw new NotImplementedException();
        }

        public void SetSelectedComponents(System.Collections.ICollection selectedComponents)
        {
            //Do nothing here
        }

        #endregion

        #region Protected Methods
        /// <summary>
        /// Raises selected changed event
        /// </summary>
        protected void OnSelectionChanged()
        {
            if (null != this.SelectionChanged)
                this.SelectionChanged(this, new EventArgs());
        }
        /// <summary>
        /// Rasises selection changing event
        /// </summary>
        protected void OnSelectionChanging()
        {
            if (null != this.SelectionChanging)
                this.SelectionChanging(this, new EventArgs());
        }

        #endregion
    }
}
